package grouplife::Controller::University;
use Moose;
use Encode qw(decode);
use namespace::autoclean;
use Catalyst::Log;
use grouplife::Model::Users::UsersSearchModel;
use grouplife::Model::Tuan::TuansSearchModel;
use grouplife::Model::UserTuanStores::UserTuanStoresModel;
use grouplife::Model::Topic::TopicSearchModel;
use grouplife::Model::Store::StoreUtilModel;
use grouplife::Model::Category::CategoryModel;
use Date::Parse;
use Time::Seconds;
use Time::Piece;

use POSIX qw(strftime);

BEGIN {extends 'Catalyst::Controller'; }
__PACKAGE__->config( 'Plugin::ConfigLoader' => { file => 'grouplife.yml' } );

=head1 NAME

grouplife::Controller::Tuans - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;

    $c->response->body('Matched grouplife::Controller::University in University.');
}

sub base :Chained('/') :PathPart('university') :CaptureArgs(0) {
	my ($self, $c) = @_;

	$c->stash(resultset => $c->model('DB::University'));
}

sub object :Chained('base') :PathPart('id') :CaptureArgs(1) {
        my ($self, $c, $id) = @_;

        $c->stash(object => $c->stash->{resultset}->find($id));

        die "Tuan $id not found!" if !$c->stash->{object};

}

sub university_info :Chained('object') :PathPart('university_info') :Args(0) {
        my ($self, $c) = @_;

	$c->cache_page( $c->config->{time_out} );
	my $university_id = $c->stash->{object}->id;
	my $cat_id = $c->request->params->{cat_id} || 'null';
	my $guapai_topics = $c->model('Topic::TopicSearchModel')->topic_search_by_univ_cat(1,$university_id,7,3);
        my @categories = $c->model('Category::CategoryModel')->get_all();
	my %category_count;
	foreach my $category (@categories){
		$category_count{$category->id} = $c->model('Tuan::TuansSearchModel')->find_ongoing_stores_count_for_university($university_id, $category->id);
	}


        my $top_ten_topics = $c->model('Topic::TopicSearchModel')->get_topic_for_university($university_id, 100);
	my $universities = $c->model('University::UniversityModel')->get_all;
	my $users = $c->model('Users::UsersSearchModel')->get_random_users_by_university($university_id);
        $c->stash(login_user => $c->session->{login_user});
        $c->stash(univ => $c->stash->{object});
        $c->stash(universities => $universities);
        $c->stash(image => $c->stash->{object}->image);
        $c->stash(topics => $top_ten_topics);
        $c->stash(users => $users);
        $c->stash(cat_id => $cat_id);
        $c->stash(category_count => \%category_count);
        $c->stash(css => 'university_mainpage.css');
        $c->stash(title => $c->stash->{object}->name);
	$c->stash(university_id => $university_id);
	$c->stash(categories => \@categories);
	$c->stash(guapai_topics => $guapai_topics);
	$c->stash(javascript => '/university/university_main.js');
        $c->stash(template => 'university/university_mainpage.tt2');
}

sub stores_total:Local {
        my ($self, $c) = @_;
	my $university_id = $c->request->params->{university_id} || 'null';
	my $cat_id = $c->request->params->{cat_id} || 'null';
	$c->res->output($c->model('Tuan::TuansSearchModel')->find_ongoing_stores_count_for_university($university_id, $cat_id));
}

sub stores_paging :Local {
        my ($self, $c) = @_;

        my $start_index = $c->request->params->{start_index};
        my $end_index = $c->request->params->{end_index};
	my $university_id = $c->request->params->{university_id} || 'null';
	my $cat_id = $c->request->params->{cat_id} || 'null';

	my $ongoings = $c->model('Tuan::TuansSearchModel')->find_ongoing_tuans_for_university($university_id);
        my $tuanTimes_hash = $c->model('Store::StoreUtilModel')->calculate_yuji_tihuo_time($ongoings);

        my $page = $start_index / 30;
        $page = $page + 1;

	$c->stash(tuanTimes_hash => $tuanTimes_hash);
        $c->stash(nowrapper => 1);
        $c->stash(login_user => $c->session->{login_user});
        $c->stash(template => 'university/university_list.tt2');
	$c->stash(ongoing_stores => [$c->model('Tuan::TuansSearchModel')->find_ongoing_stores_for_university($page, $university_id, $cat_id)]);
}


=head1 AUTHOR


lijie,,,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

